package service;

import domain.Cart;
import domain.Order;
import domain.Product;
import persistence.OrderDAO;
import persistence.impl.OrderDAOImpl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

public class OrderService {
    private OrderDAO orderDAO;

    public OrderService(){
        this.orderDAO = new OrderDAOImpl();
    }

    public List<Order> getOrdersOfUser(String userid){
        return orderDAO.getOrdersByUserid(userid);
    }

    public Order productToOrder(Product product){
        Order order = new Order();
        order.setName(product.getSpecies() + "-" + product.getBreed());
        order.setTime(new Timestamp(System.currentTimeMillis()));
        order.setProductid(product.getProductId());
        order.setUnitprice(product.getUnitprice());
        order.setImageSrc(product.getImageSrc());
        order.setOrderid(String.valueOf(System.currentTimeMillis()));
        return order;
    }

    public List<Order> cartToOrder(List<Cart> cartList){
        List<Order> orderList = new ArrayList<>();
        for(Cart cart : cartList){
            Order order = new Order();
            order.setName(cart.getName());
            order.setNum(cart.getNum());
            order.setImageSrc(cart.getImageSrc());
            order.setUserid(cart.getUserid());
            order.setProductid(cart.getProductid());
            order.setTime(new Timestamp(System.currentTimeMillis()));
            order.setOrderid(String.valueOf(System.currentTimeMillis()));
            order.setImageSrc(cart.getImageSrc());
            order.setUnitprice(cart.getUnitprice());
            order.setPrice(cart.getPrice());
            orderList.add(order);
        }
        return orderList;
    }

    public Order getOrder(String orderid){
        return orderDAO.getOrderByOrderid(orderid);
    }

    public boolean addOrder(Order order){
        return orderDAO.addOrder(order);
    }

    public boolean deleteOrder(String orderid) {return orderDAO.delOrder(orderid);}
}
